﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Xml;
using System.Web.SessionState;
using MyLibrary.API.Json;

namespace MyLibrary.API.Web
{
    public class MyHandler : IHttpHandler, IRequiresSessionState
    {
        private HttpContext context;

        protected HttpContext Context{ get { return this.context; } }

        protected HttpRequest Request{ get { return Context.Request; } }

        protected HttpResponse Response{ get { return Context.Response; } }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                this.context = context;
                if (Request["cmd"] != null)
                {
                    if (Request["admin"] != null && Convert.ToBoolean(Request["admin"]) == true)
                    {
                        ProcessAdmin(Request["cmd"].Trim().ToLower());
                        return;
                    }
                    string cmd = Request["cmd"].Trim().ToLower();                    
                    ProcessCommand(cmd);
                }
                else
                {
                    Failure("An invalid command was passed in the response");
                }
            }
            catch (Exception error)
            {
                Failure(error.ToString());
            }
        }

        protected virtual void ProcessCommand(string cmd)
        {
            Failure("No functionality has been enabled for this proxy.");
        }

        protected virtual void ProcessAdmin(string cmd)
        {
            Failure("No functionality has been enable for this proxy.");
        }

        protected void Success(string message)
        {
            Flush(JsonParser.Success(message));
        }

        protected void Failure(string message)
        {
            Flush(JsonParser.Failure(message));
        }

        protected void Success<T>(string message, List<T> data) where T : class
        {
            Flush(JsonParser.Success(message, data));
        }

        protected void Success<T>(string message, List<T> data, int start, int limit) where T : class
        {
            Flush(JsonParser.Success(message, data, start, limit));
        }

        protected void Success(string docpath, string message, string children)
        {
            try
            {
                string fullpath = Context.Server.MapPath(docpath);
                Flush(JsonParser.Success(fullpath, message, children));
            }
            catch (Exception error)
            { 
                Failure(error.ToString());
            }
        }

        protected void Flush(string message)
        {
            Response.ContentType = "text/javascript";
            Response.Write(message);
            Response.Flush();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
